package com.ztjy.market.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.bean.Orderdetails;
import com.ztjy.bean.Orders;
import com.ztjy.bean.Users;
import com.ztjy.market.biz.OrderdetailsBiz;
import com.ztjy.market.biz.OrdersBiz;
import com.ztjy.sys.biz.UsersBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/market/orderExamine")
public class orderExamineController {
    @Autowired
    private OrdersBiz ordersBiz;
    @Autowired
    private OrderdetailsBiz odbiz;
    @Autowired
    private UsersBiz ubiz;

    @RequestMapping("/orderExamineAll")
    public String orderExamineList(Model model,Orders orders, @RequestParam(value = "pageno", defaultValue = "1") Integer pageNo,
                                   String ordertimemin, String ordertimemax, Double ordermoneyMin, Double ordermoneyMax) {
        Date ordertimeMin = null;
        if (ordertimemin != null && !ordertimemin.equals("")) {
            ordertimeMin = Date.valueOf(ordertimemin);
        }
        Date ordertimeMax = null;
        if (ordertimemax != null && !ordertimemax.equals("")) {
            ordertimeMax = Date.valueOf(ordertimemax);
        }
        Integer pageSize = 4;
        orders.setStatus("1");
        PageInfo<Orders> pageInfo = ordersBiz.findAllOrders(orders, pageNo, pageSize, ordertimeMin, ordertimeMax, ordermoneyMin, ordermoneyMax);
        model.addAttribute("Opinfo", pageInfo);
        model.addAttribute("orders", orders);
        model.addAttribute("ordertimeMin", ordertimeMin);
        model.addAttribute("ordertimeMax", ordertimeMax);
        model.addAttribute("ordermoneyMin", ordermoneyMin);
        model.addAttribute("ordermoneyMax", ordermoneyMax);
        return "forward:/market/orderExamine/orderExamineList.html";
    }
    @RequestMapping("/goAuditOrders")
    public String goAuditOrders(String orderid,Model model){
        Orders orders = ordersBiz.findOrderDetails(orderid);
        List<Orderdetails> orderdetailsList = odbiz.findByOrderId(orderid);
        model.addAttribute("order",orders);
        model.addAttribute("olist",orderdetailsList);
        return "forward:/market/orderExamine/orderExamine.html";
    }
    @RequestMapping("/doAuditOrders")
    public String doAuditOrders(Orders orders, HttpSession session){
        Users users = (Users) session.getAttribute("currUser");
        orders.setCheckid(users.getuId());
        orders.setChectime(new Date(System.currentTimeMillis()));
        boolean flag=false;
        flag=ordersBiz.updateOrders(orders);
        if (flag){
            return "redirect:/market/orderExamine/orderExamineAll";
        }else {
            return "";
        }
    }


}
